package com.tanhua.admin.controller;

import com.sun.xml.bind.v2.model.core.ID;
import com.tanhua.admin.service.ManagerService;
import com.tanhua.model.domain.UserInfo;
import com.tanhua.model.vo.PageResult;
import net.coobird.thumbnailator.util.exif.IfdStructure;
import org.apache.commons.lang.StringUtils;
import org.bouncycastle.asn1.x500.style.RFC4519Style;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

import static org.bouncycastle.asn1.x500.style.RFC4519Style.uid;

@RestController
@RequestMapping("/manage")
public class ManagerController {

    @Autowired
    private ManagerService managerService;


    @GetMapping("/users")
    public ResponseEntity users(@RequestParam(defaultValue = "1") Integer page,
                                @RequestParam(defaultValue = "10") Integer pagesize) {
        PageResult result = managerService.findAllUsers(page,pagesize);
        return ResponseEntity.ok(result);
    }
    @GetMapping("/users/{userId}")
    public ResponseEntity findUserById(@PathVariable("userId") Long userId) {
        UserInfo userInfo = managerService.findUserById(userId);
        return ResponseEntity.ok(userInfo);
    }

    @GetMapping("/messages")
    public ResponseEntity messages(@RequestParam Map map) {
        String page = (String) map.get("page");
        Integer integer = Integer.valueOf(page);
        if (integer==null){
            integer =1;
        }
        String pagesize = (String) map.get("pagesize");
        Integer valueOf = Integer.valueOf(pagesize);
        if (valueOf==null){
            valueOf=10;
        }
        String uid = (String) map.get("uid");
        Long id =null;
        Long sid = null;
        if (uid!=null){
            id = Long.valueOf(uid);
        }
        if (id !=null){
             sid = id;
        }
        if (uid ==null){
           id =sid;
        }
        String state = (String) map.get("state");
            if ((state==null||state.equals("''"))||state.equals("")) {
                state = "0";
            }
        Integer statel = Integer.valueOf(state);
        PageResult result = managerService.findAllMovements(integer,valueOf, id,statel);
        return ResponseEntity.ok(result);
    }

    @GetMapping("/videos")
    public ResponseEntity videos(@RequestParam(defaultValue = "1") Integer page,
                                 @RequestParam(defaultValue = "10") Integer pagesize,
                                 Long uid ) {
        PageResult result = managerService.findAllVideos(page,pagesize,uid);
        return ResponseEntity.ok(result);
    }

    @PostMapping("/users/freeze")
    public ResponseEntity freeze(@RequestBody Map params) {
        Map map =  managerService.userFreeze(params);
        return ResponseEntity.ok(map);
    }

    @PostMapping("/users/unfreeze")
    public ResponseEntity unfreeze(@RequestBody  Map params) {
        Map map =  managerService.userUnfreeze(params);
        return ResponseEntity.ok(map);
    }
}
